package zb.ex;

import java.util.concurrent.TimeUnit;

/**
 * @author wzby
 * @Date: 2022/8/8 20:36
 */
public class Ticket implements Runnable {


    public static void main(String[] args) {
        // 模拟售票
        Ticket ti = new Ticket(10000);
        new Thread(ti::run,"嘉禾望岗").start();
        new Thread(ti,"金台夕照").start();
        new Thread(ti,"安河桥北").start();
        new Thread(ti,"紫荆山").start();
        new Thread(ti,"橘子洲").start();
    }
    private int num = 100;


    public Ticket(int num){
        this.num = num;
    }

    @Override
    public void run() {
        String t = Thread.currentThread().getName();
        System.out.printf("%s开始售票 %n",t);
        while (true){
            synchronized (this){
                if (this.num < 1){
                    break;
                }else {
                    try {
                        System.out.printf("%s:售出一张票(座号:%d)，剩余张%d票 %n",t,num,--num);
                        //TimeUnit.SECONDS.sleep();
                    }catch (Exception e){

                    }
                }
            }
        }
    }
}
